package com.fc.app.fcapp.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.fc.library.fresco.Util;

import java.util.List;

/**
 * Created by fc on 2018/5/21.
 */


public class PathShadowView extends View {
    private Paint mPaint;
    private Path mPath;
    private List<Point> mPointList;

    public PathShadowView(Context context) {
        super(context);
        init();
    }

    private void init() {
        // 创建一个画刷
        mPaint = new Paint();
        // 设置画刷属性
        mPaint.setAntiAlias(true);           // 去锯齿
        // 修改画刷属性
        mPaint.setStyle(Paint.Style.FILL);   // 设置画刷样式为实心

        mPath = new Path();
    }

    public void setPointList(List<Point> pointList) {
        mPointList = pointList;

        if (mPointList != null && mPointList.size() > 0) {
            mPath.moveTo(mPointList.get(0).getX(), mPointList.get(0).getY());
            mPaint.setShader(
                    new LinearGradient(Util.Div(mPointList.get(0).getX()), getMinY(),// 最高(值最小)的那个Y
                            Util.Div(mPointList.get(0).getX()), Util.Div(mPointList.get(0).getY()),
                            mStartColor, mEndColor, Shader.TileMode.CLAMP));


            for (int i = 1; i < mPointList.size(); i++) {
                mPath.lineTo(Util.Div(mPointList.get(i).getX()), Util.Div(mPointList.get(i).getY()));
            }
            mPath.lineTo(Util.Div(mPointList.get(mPointList.size() - 1).getX()), Util.Div(mPointList.get(0).getY()));
            mPath.lineTo(Util.Div(mPointList.get(0).getX()), Util.Div(mPointList.get(0).getY()));
            mPath.close();
        }

    }

    private Point mFirstPoint, mLastPoint;//上一个坐标
    private int mStartColor, mEndColor;

    public void setPainColor(int startColor, int endColor) {
        mStartColor = startColor;
        mEndColor = endColor;
    }

    /**
     * 如果是一个点一个点回来的坐标可以调用此方法
     * @param p
     * @param isFirstPoint
     * @param maxPoint
     */
    public void setPoint(Point p, boolean isFirstPoint,Point maxPoint) {
        if (p != null) {
            if (isFirstPoint) {
                mLastPoint = mFirstPoint = p;
            }
            mPaint.setShader(
                    new LinearGradient(Util.Div(mFirstPoint.getX()), maxPoint.getY(),// 最高(值最小)的那个Y
                            Util.Div(mFirstPoint.getX()), Util.Div(mFirstPoint.getY()),
                            mStartColor, mEndColor, Shader.TileMode.CLAMP));
            Log.i("PathShadowView", " mLastPoint: " + JSON.toJSONString(mLastPoint) + "  p " + JSON.toJSONString(p));
            mPath.moveTo(Util.Div(mLastPoint.getX()), mLastPoint.getY());
            mPath.lineTo(Util.Div(p.getX()), Util.Div(p.getY()));
            mPath.lineTo(Util.Div(p.getX()), Util.Div(mFirstPoint.getY()));
            mPath.lineTo(Util.Div(mLastPoint.getX()), Util.Div(mFirstPoint.getY()));
            mPath.close();
            invalidate();
            mLastPoint = p;
        }
    }

    private int getMinY() {
        int min = 0;
        if (mPointList != null && mPointList.size() > 0) {
            min = mPointList.get(0).getY();
            for (int i = 0; i < mPointList.size(); i++) {
                if (mPointList.get(i).getY() < min) {
                    min = mPointList.get(i).getY();
                }
            }
        }
        return Util.Div(min);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 画一个渐变色梯形
        canvas.drawPath(mPath, mPaint);
    }
}